package io.renren.modules.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.entity.*;
import io.renren.common.enums.CashtypeEnum;
import io.renren.common.enums.CircleLevelEnum;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.demo.dao.XlsLogCashDao;
import io.renren.modules.demo.dao.XlsUserAwardDao;
import io.renren.modules.demo.dao.XlsUsersDrawsDao;
import io.renren.modules.demo.dto.XlsUserAwardDTO;
import io.renren.modules.demo.dto.XlsUsersDrawsDTO;
import io.renren.modules.demo.service.XlsUsersDrawsService;
import io.renren.modules.security.entity.SysUserTokenEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 用户提现记录
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2020-04-16
 */
@Service
public class XlsUsersDrawsServiceImpl extends CrudServiceImpl<XlsUsersDrawsDao, XlsUsersDrawsEntity, XlsUsersDrawsDTO> implements XlsUsersDrawsService {

    @Autowired
    private XlsLogCashDao xlsLogCashDao;

    @Autowired
    private XlsUserAwardDao xlsUserAwardDao;

    @Override
    public QueryWrapper<XlsUsersDrawsEntity> getWrapper(Map<String, Object> params) {

        QueryWrapper<XlsUsersDrawsEntity> wrapper = new QueryWrapper<>();

        return wrapper;
    }

    @Override
    public PageData<XlsUsersDrawsDTO> queryPage(Map<String, Object> params) {
        QueryWrapper<XlsUsersDrawsEntity> wrapper = getWrapper(params);
        if (ObjectUtil.isNotEmpty(params.get("mobile"))) {
            wrapper.eq("xu.mobile", params.get("mobile"));
        }
        if (ObjectUtil.isNotEmpty(params.get("username"))) {
            wrapper.like("xu.username", params.get("username"));
        }
        IPage<XlsUsersDrawsDTO> page = baseDao.queryPage(
                getPage(params, null, false),
                wrapper
        );

        return getPageData(page, currentDtoClass());
    }

    @Override
    public XlsUsersDrawsDTO selectXlsUsersDrawsById(Long id) {
        return baseDao.selectXlsUsersDrawsById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateXlsUsersDrawsStatus(SysUserTokenEntity byToken, XlsUsersDrawsDTO dto, boolean isPass) {
        Long userId = byToken.getUserId();
        XlsUsersDrawsEntity xlsUsersDrawsEntity = XlsUsersDrawsEntity.builder()
                .cashStatus(isPass == true ? 3 : -1)
                .checkStaffid(userId)
                .checkRemark(isPass == true ? "" : dto.getCheckRemark())
                .checkTime(new Date())
                .build();
        xlsUsersDrawsEntity.setId(dto.getId());
        this.updateById(xlsUsersDrawsEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updataAuditPass(SysUserTokenEntity byToken, XlsUsersDrawsDTO dto, boolean isPass) {
        this.updateXlsUsersDrawsStatus(byToken, dto, isPass);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updataAuditRefused(SysUserTokenEntity byToken, XlsUsersDrawsDTO dto, boolean isPass) {
        this.updateXlsUsersDrawsStatus(byToken, dto, isPass);
        //提现手续费退回
        insertCashLog(dto,CashtypeEnum.WITHDRAWAL_FEE_REFUNDED.value());
        //返回用户手续费
        xlsUserAwardDao.updateAward(Long.parseLong(dto.getUserId()), dto.getTaxratio());
        //提现金额退回
        insertCashLog(dto,CashtypeEnum.WITHDRAWAL_AMOUNT_REFUNDED.value());
        //奖励金
        xlsUserAwardDao.updateAward(Long.parseLong(dto.getUserId()), dto.getCash());
    }

    /**
     * 添加奖励金额记录
     * @param dto
     * @param type 现金类型
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertCashLog( XlsUsersDrawsDTO dto,int type) {
        long userId = Long.parseLong(dto.getUserId());
        //获取奖励前余额
        XlsUserAwardEntity userAwardEntity = xlsUserAwardDao.selectUserAwardByUserId(userId);
        BigDecimal preCash = new BigDecimal(0);
        if (ObjectUtil.isNotEmpty(userAwardEntity)) {
            preCash = userAwardEntity.getAwardPrice();
        }
        //添加奖励金额记录
        xlsLogCashDao.insert(XlsLogCashEntity.builder()
                .cashType(1)
                .createTime(new Date())
                .dataFlag(1)
                .fromId(1L)
                .userId(userId)
                .cash(type==CashtypeEnum.WITHDRAWAL_FEE_REFUNDED.value() ? dto.getTaxratio() : dto.getCash())
                .orderNo(dto.getDrawNo())
                .preCash(preCash)
                .remark(type==CashtypeEnum.WITHDRAWAL_FEE_REFUNDED.value() ? "提现手续费退回" : "提现金额退回")
                .type(type)
                .build());
    }
}